﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AG.Editor.UI;
using AG.Editor.MapUI.Windows;
using AG.Data;
using AGMapEditor.Controls;
using AG.MapEditor.Data;
using AGMapEditor.Windows;

namespace AG.Editor.MapUI
{
    public partial class AGMapMgrPanel : UserControl, IAGEMainComponent
    {
        AGEMapPanel _ctlMapPanel;
        AGSelectUnitPanel _ctlSelUnitPanel;
        AGSelectTerrainPanel _ctlSelTerrainPanel;
        AGMapObjectPanel _ctlObjPanel;
        AGSwitchPointPanel _ctlSPPanel;

        AGMapEditToolbar _ctlMapToolbar;
        PanelEx panel2;

        public AGMapMgrPanel()
        {
            InitializeComponent();

            _ctlMapPanel = new AGEMapPanel();
            //_ctlMapPanel.Dock = DockStyle.Fill;
            panel2 = new PanelEx();
            panel2.Dock = DockStyle.Fill;
            panel3.Controls.Add(panel2);
            panel2.Controls.Add(_ctlMapPanel);
            panel2.Click += new EventHandler(panel2_Click);
            panel2.Scroll += new ScrollEventHandler(panel2_Scroll);
            panel2.AutoScroll = true;

            _ctlSelUnitPanel = new AGSelectUnitPanel(_ctlMapPanel);
            _ctlSelUnitPanel.Dock = DockStyle.Fill;
            splitContainer2.Panel1.Controls.Add(_ctlSelUnitPanel);

            _ctlSelTerrainPanel = new AGSelectTerrainPanel(_ctlMapPanel);
            _ctlSelTerrainPanel.Dock = DockStyle.Fill;
            splitContainer2.Panel2.Controls.Add(_ctlSelTerrainPanel);

            _ctlMapToolbar = new AGMapEditToolbar(_ctlMapPanel);
            _ctlMapToolbar.Dock = DockStyle.Fill;
            panel1.Controls.Add(_ctlMapToolbar);

            _ctlObjPanel = new AGMapObjectPanel(_ctlMapPanel);
            _ctlObjPanel.Dock = DockStyle.Fill;
            splitContainer4.Panel1.Controls.Add(_ctlObjPanel);

            _ctlSPPanel = new AGSwitchPointPanel(_ctlMapPanel);
            _ctlSPPanel.Dock = DockStyle.Fill;
            splitContainer4.Panel2.Controls.Add(_ctlSPPanel);
        }

        void panel2_Click(object sender, EventArgs e)
        {
        }

        void panel2_Scroll(object sender, ScrollEventArgs e)
        {
            if (e.ScrollOrientation == ScrollOrientation.HorizontalScroll)
            {
                _ctlMapPanel.OffsetPointX = e.NewValue;
            }
            else if (e.ScrollOrientation == ScrollOrientation.VerticalScroll)
            {
                _ctlMapPanel.OffsetPointY = e.NewValue;
            }

            panel2.AutoScrollPosition = new Point(_ctlMapPanel.OffsetPointX, _ctlMapPanel.OffsetPointY);
        }

        public void OnActived(AGEMainMenuMidiator mmm)
        {
            mmm.Clear();

            ToolStripMenuItem mFile = mmm.AddMenu(new ToolStripMenuItem("文件"));
            ToolStripMenuItem miSaveProject = new ToolStripMenuItem("保存");
            miSaveProject.Click += new EventHandler(miSaveProject_Click);
            mFile.DropDownItems.Add(miSaveProject);

            ToolStripMenuItem miOpenMap = new ToolStripMenuItem("打开");
            miOpenMap.Click += new EventHandler(miOpenMap_Click);
            mFile.DropDownItems.Add(miOpenMap);

            ToolStripMenuItem miClose = new ToolStripMenuItem("关闭");
            //miClose.Click += new EventHandler(miClose_Click);
            mFile.DropDownItems.Add(miClose);

            ToolStripMenuItem m1 = mmm.AddMenu(new ToolStripMenuItem("地图管理"));
            ToolStripMenuItem miCreateMap = new ToolStripMenuItem("创建地图");
            miCreateMap.Click += new EventHandler(miCreateMap_Click);
            m1.DropDownItems.Add(miCreateMap);

            ToolStripMenuItem m3 = mmm.AddMenu(new ToolStripMenuItem("对象管理"));
            ToolStripMenuItem miDeleteObj = new ToolStripMenuItem("删除对象");
            miDeleteObj.Click += new EventHandler(miDeleteObj_Click);
            m3.DropDownItems.Add(miDeleteObj);

            ToolStripMenuItem miEditObj = new ToolStripMenuItem("编辑对象");
            miEditObj.Click += new EventHandler(miEditObj_Click);
            m3.DropDownItems.Add(miEditObj);



            ToolStripMenuItem m4 = mmm.AddMenu(new ToolStripMenuItem("地图切换点管理"));
            ToolStripMenuItem miCreateSwitchPoint = new ToolStripMenuItem("创建切换点");
            miCreateSwitchPoint.Click += miCreateSwitchPoint_Click;
            m4.DropDownItems.Add(miCreateSwitchPoint);
            ToolStripMenuItem miEditSwitchPoint = new ToolStripMenuItem("编辑切换点");
            miEditSwitchPoint.Click += miEditSwitchPoint_Click;
            m4.DropDownItems.Add(miEditSwitchPoint);
        }

        void miEditSwitchPoint_Click(object sender, EventArgs e)
        {
            if (_ctlMapPanel.Map == null)
            {
                return;
            }

            if (_ctlMapPanel.SelectedSwitchPoint == null)
            {
                return;
            }

            AGEditSwitchPointWindow window = new AGEditSwitchPointWindow(_ctlMapPanel.Map, _ctlMapPanel.SelectedSwitchPoint);
            if (window.ShowDialog() == DialogResult.OK)
            {
                _ctlMapPanel.UpdateSwitchPoint(window.EditedSwitchPoint);
            }
        }

        void miCreateSwitchPoint_Click(object sender, EventArgs e)
        {
            if (_ctlMapPanel.Map == null)
            {
                return;
            }

            AGCreateSwitchPointWindow window = new AGCreateSwitchPointWindow(_ctlMapPanel.Map);
            if (window.ShowDialog() == DialogResult.OK)
            {
                _ctlMapPanel.AddSwitchPoint(window.CreatedSwitchPoint);
            }
        }

        void miEditObj_Click(object sender, EventArgs e)
        {
            if (_ctlMapPanel.SelectedObject != null)
            {
                AGEditObjectWindow editWindow = new AGEditObjectWindow(_ctlMapPanel.SelectedObject);
                if (editWindow.ShowDialog() == DialogResult.OK)
                {
                    _ctlMapPanel.RefreshObjects();
                }
            }
        }

        void miOpenMap_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                AGMap map = AGMEContext.Current.MapStore.LoadMap(dlg.FileName);
                _ctlMapPanel.SetMap(map);

                this.ParentForm.Text = map.Name;
                //_ctlHScrollBar.Maximum = _ctlMapPanel.MapSize.Width;
                //_ctlVScrollBar.Maximum = _ctlMapPanel.MapSize.Height;
            }
        }

        void miSaveProject_Click(object sender, EventArgs e)
        {
            if (_ctlMapPanel.Map != null)
            {
                AGMEContext.Current.MapStore.SaveMap(string.Format("{0}Data\\Maps\\", AppDomain.CurrentDomain.BaseDirectory),
                    _ctlMapPanel.Map);
                MessageBox.Show("保存完毕", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        void miCreateMap_Click(object sender, EventArgs e)
        {
            AGECreateMapWindow window = new AGECreateMapWindow();
            if (window.ShowDialog() == DialogResult.OK)
            {
                AGMap map = window.Map;
                _ctlMapPanel.SetMap(map);
                
                this.ParentForm.Text = map.Name;

                //_ctlHScrollBar.Maximum = _ctlMapPanel.MapSize.Width;
                //_ctlVScrollBar.Maximum = _ctlMapPanel.MapSize.Height;
                /*
                AGEContext.Current.EProject.AddMap(map);
                AGTools.Current.MapStore.SaveMap(AGEContext.Current.EProject, map);
                BindMapTree();
                 * */
            }
        }

        private void _ctlTreeMap_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Node != null)
            {
                /*
                AGMapRef mapRef = e.Node.Tag as AGMapRef;
                if (mapRef != null)
                {
                    AGMap map = AGTools.Current.MapStore.LoadMap(AGEContext.Current.EProject, mapRef);
                    _ctlMapPanel.SetMap(map);
                }
                 * */
            }
        }

        void miDeleteObj_Click(object sender, EventArgs e)
        {
            if (_ctlMapPanel.SelectedObject != null)
            {
                if (MessageBox.Show(string.Format("是否删除对象[{0}]?", _ctlMapPanel.SelectedObject.Caption),
                    "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                {
                    _ctlMapPanel.DeleteObject(_ctlMapPanel.SelectedObject);
                }
            }
        }
    }

    class PanelEx : Panel
    {
        protected override Point ScrollToControl(Control activeControl)
        {
            return this.AutoScrollPosition;// base.ScrollToControl(activeControl);
        }
    }
}
